﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WCFAuthorization.Server
{
    public partial class FormServer : Form
    {
        Server MyServer = null;

        public FormServer()
        {
            InitializeComponent();

            MyServer = new Server();
            MyServerShow.SelectedObject = MyServer;
        }

        private void FormServer_Load(object sender, EventArgs e)
        {
            this.btnStop.Enabled = false;
        }

        private void FormServer_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult diag = MessageBox.Show("是否关闭服务器?",
               "提示",
               MessageBoxButtons.YesNo,
               MessageBoxIcon.Question);

            if (diag == DialogResult.No)
            {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
            }

            MyNotify.Dispose();
        }

        private void MyNotify_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            string message;
            if (MyServer.Start(out message))
            {
                btnStop.Enabled = true;
                btnStart.Enabled = false;

                MyServerShow.Enabled = false;
            }
            else
                MessageBox.Show(message);   
        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            MyServer.Stop();
            MyServerShow.Enabled = true;
            btnStart.Enabled = true;
            btnStop.Enabled = false;
        }

        private void btnShowUserManager_Click(object sender, EventArgs e)
        {
            using (FormLogin login = new FormLogin())
            {
                if (login.ShowDialog() != DialogResult.OK)
                {
                    MessageBox.Show("没有权限!");
                }
                else
                {
                    bool power = login.IsPower;
                    using (FormUserManager form = new FormUserManager(power))
                    {
                        form.ShowDialog();
                    }
                }
            }
        }
    }
}